package com.hongsheng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hongsheng.pojo.Line;
import com.hongsheng.pojo.R;
import com.hongsheng.service.LineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ClassName:LineController
 * Description:
 *
 * @Author: 曾晋冠
 * @Version: 2022.3.2
 * @Create: 2023/8/12 - 13:24
 */
@CrossOrigin
@RestController
@RequestMapping("/line")
public class LineController {
    @Autowired
    private LineService lineService;

    /**
     * 查询全部线路
     * @return
     */
    @GetMapping
    public R findAllLine(){
        List<Line> lineList = lineService.findAllLine();
        return new R(true,lineList);
    }

    /**
     * 添加
     * @param line
     * @return
     */
    @PostMapping
    public R insertLine(@RequestBody Line line){
        Boolean aBoolean = lineService.insertLine(line);
        return new R(true,aBoolean);
    }

    /**
     * 修改
     * @param line
     * @return
     */
    @PutMapping
    public R updateLine(@RequestBody Line line){
        Boolean aBoolean = lineService.updateLine(line);
        return new R(true ,aBoolean);
    }


    /**
     *   通过id查询
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteLine(@PathVariable Integer id){
        return new R(true,lineService.deleteLine(id));
    }
    /**
     * 通过id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findAllById(@PathVariable Integer id){
        return new R(true,lineService.findAllById(id));
    }
    @GetMapping("/{currentPage}/{pageSize}")
    public R getPageLine(@PathVariable Integer currentPage,@PathVariable Integer pageSize,Line line){
       IPage<Line> iPage= lineService.getPageLine(currentPage,pageSize,line);
       if (currentPage>iPage.getPages()){
          iPage= lineService.getPageLine((int) iPage.getPages(),pageSize,line);
       }
       return new R(true,iPage);
    }
}
